home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / wdreg.cpp < prev    next >
C/C++ Source or Header  |  2001-04-11  |  10KB  |  368 lines

  1. #include <windows.h>
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include "drvload.h"
  5. #include "../../include/windrvr.h"
  6.  
  7. int main (int argc, char *argv[]);
  8.  
  9. #ifndef _CONSOLE
  10.     char g_sAppName[256];
  11.     char g_sBuf[4096];
  12.     
  13.     void OutputMessage()
  14.     {
  15.         MessageBox( NULL, g_sBuf, g_sAppName, MB_OK | MB_ICONINFORMATION );
  16.     }
  17.  
  18.     int printf( char *szFormat, ... )
  19.     {
  20.         char sBuf[256];
  21.         DWORD ret;
  22.         va_list marker;
  23.         va_start( marker, szFormat );
  24.         ret = vsprintf( sBuf, szFormat, marker );
  25.         strcat( g_sBuf, sBuf );
  26.         va_end( marker );
  27.         return ret;
  28.     }
  29.  
  30.     int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, int iCmdShow)
  31.     {
  32.         char argv0[256];
  33.     
  34.         char *argv_all;
  35.         char *argv[256];
  36.         int ret_val;
  37.         int argc = 0;
  38.     
  39.         BOOL fInArg = FALSE;
  40.         BOOL fInQuote;
  41.         int i,j;
  42.     
  43.         GetModuleFileName(hInstance, argv0, sizeof (argv0));
  44.         strcpy( g_sAppName, argv0 );
  45.         argv[argc++] = argv0;
  46.     
  47.         argv_all = (char *) malloc (strlen(szCmdLine)+1);
  48.     
  49.         j = 0;
  50.         for (i=0; (char) szCmdLine[i]; i++)
  51.         {
  52.             char ch = (char) szCmdLine[i];
  53.             if (fInArg)
  54.             {
  55.                 if (fInQuote && ch=='\"' || !fInQuote && (ch==' ' || ch=='\t'))
  56.                 {
  57.                     fInArg = FALSE;
  58.                     argv_all[j++] = '\0';
  59.                 }
  60.                 else
  61.                 {
  62.                     argv_all[j++] = ch;
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 if (ch!=' ' && ch!='\t')
  68.                 {
  69.                     fInArg = TRUE;
  70.                     fInQuote = ch=='\"';
  71.                     if (!fInQuote)
  72.                         i--;
  73.                     argv[argc] = argv_all + j;
  74.                     argc++;
  75.                 }
  76.             }
  77.         }
  78.     
  79.         if (fInArg)
  80.             argv_all[j++] = '\0';
  81.     
  82.         argv[argc] = NULL;
  83.     
  84.         ret_val = main(argc, argv);
  85.  
  86.     free (argv_all);
  87.  
  88.     return ret_val;
  89. }
  90. #else
  91.     void OutputMessage()
  92.     {
  93.     }
  94. #endif //_CONSOLE
  95.  
  96. void PrintUsage(char *exe)
  97. {
  98.     printf ("%s [Options] Action\n", exe);
  99.     printf ("Options:\n");
  100. #ifndef _CONSOLE
  101.         printf ("    -silent: supresses output message box.\n");
  102. #endif //_CONSOLE
  103.     printf ("    -startup: Valid only for WinNT.\n");
  104.     printf ("              Set startup level: boot, system, automatic, manual, disabled\n");
  105.     printf ("              (default: automatic).\n");
  106.     printf ("    -name:  Set then name of the driver (default: " WD_PROD_NAME ").\n");
  107.     printf ("    -file:  Set the file name of the driver.\n");
  108.     printf ("    -vxd:   load a .vxd on Win98 (default .sys)\n");
  109.     printf ("Action:\n");
  110.     printf ("    create:  create a registry entry for the driver.\n");
  111.     printf ("    start:   start the driver.\n");
  112.     printf ("    install: create + start.\n");
  113.     printf ("    delete:  delete the registry entry for the driver.\n");
  114.     printf ("    stop:    stop the driver.\n");
  115.     printf ("    remove:  stop + delete.\n");
  116.     printf ("Examples:\n");
  117.     printf ("    To load a driver called MPEG Encoder, with file names MPEGENC.VXD and\n");
  118.     printf ("    MPEGENC.SYS:\n");
  119.     printf ("    WDREG -name \"MPEG Encoder\" -file MPEGENC -startup manual create start\n");
  120.     printf ("    is the same as:\n");
  121.     printf ("    WDREG -name \"MPEG Encoder\" -file MPEGENC -startup manual install\n");
  122.     printf ("\n");
  123.     printf ("    To unload default driver (i.e. " WD_PROD_NAME "), and remove it from registry:\n");
  124.     printf ("    WDREG stop delete\n");
  125.     printf ("    is the same as:\n");
  126.     printf ("    WDREG remove\n");
  127.     OutputMessage();
  128. }
  129.  
  130. int main (int argc, char *argv[])
  131. {
  132.     char *sDriverName = NULL;
  133.     char *sDriverFile = NULL;
  134.     BOOL fQuiet = FALSE;
  135.     BOOL fErrComLine = FALSE;
  136.     BOOL fVxd = FALSE;
  137.     char sErrBuf[1024];
  138.     DWORD startup = 2;
  139.     LoadDriver *loadDriver = NULL;
  140.     int i;
  141.  
  142.     if (argc==1)
  143.     {
  144.         PrintUsage(argv[0]);
  145.         return 0;
  146.     }       
  147.     for (i=1; i<argc; i++)
  148.     {
  149.         if (argv[i][0]=='-')
  150.         {
  151.             if (stricmp(argv[i],"-silent")==0)
  152.             {
  153.                 fQuiet = TRUE;
  154.                 continue;
  155.             }
  156.             if (stricmp(argv[i],"-vxd")==0)
  157.             {
  158.                 fVxd = TRUE;
  159.                 continue;
  160.             }
  161.             if (stricmp(argv[i],"-name")==0)
  162.             {
  163.                 i ++;
  164.                 if (i==argc)
  165.                 {
  166.                     sprintf (sErrBuf, "option '-name' needs a following argument\n");
  167.                     fErrComLine = TRUE;
  168.                     break;
  169.                 }
  170.                 sDriverName = argv[i];
  171.                 continue;
  172.             }
  173.             if (stricmp(argv[i],"-startup")==0)
  174.             {
  175.                 i ++;
  176.                 if (i==argc)
  177.                 {
  178.                     sprintf (sErrBuf, "option '-startup' needs a following argument\n");
  179.                     fErrComLine = TRUE;
  180.                     break;
  181.                 }
  182.                 if (stricmp(argv[i],"boot")==0)
  183.                     startup = SERVICE_BOOT_START;
  184.                 else if (stricmp(argv[i],"system")==0)
  185.                     startup = SERVICE_SYSTEM_START;
  186.                 else if (stricmp(argv[i],"automatic")==0)
  187.                     startup = SERVICE_AUTO_START;
  188.                 else if (stricmp(argv[i],"manual")==0)
  189.                     startup = SERVICE_DEMAND_START;
  190.                 else if (stricmp(argv[i],"disabled")==0)
  191.                     startup = SERVICE_DISABLED;
  192.                 else
  193.                 {
  194.                     sprintf (sErrBuf,"option '-startup' needs one of the following argument:\n");
  195.                     sprintf (sErrBuf, "  boot, system, automatic , manual, disabled\n");
  196.                     fErrComLine = TRUE;
  197.                     break;
  198.                 }
  199.                 continue;
  200.             }
  201.             else if (stricmp(argv[i],"-file")==0)
  202.             {
  203.                 i ++;
  204.                 if (i==argc)
  205.                 {
  206.                     sprintf (sErrBuf, "option '-file' needs a following argument\n");
  207.                     fErrComLine = TRUE;
  208.                     break;
  209.                 }
  210.                 sDriverFile = argv[i];
  211.                 continue;
  212.             }
  213.             else
  214.             {
  215.                 sprintf (sErrBuf, "unknown option %s\n",argv[i]);
  216.                 fErrComLine = TRUE;
  217.                 break;
  218.             }
  219.         }
  220.         else break;
  221.     }
  222.     if( fErrComLine )
  223.     {
  224.         printf( "%s", sErrBuf );
  225.         if (!fQuiet)
  226.             OutputMessage();
  227.  
  228.         return EXIT_FAILURE;
  229.     }
  230.     if (!sDriverName && !sDriverFile)
  231.     {
  232.         sDriverName =  WD_PROD_NAME;
  233.         sDriverFile = "WINDRVR";
  234.     }
  235.     else if (!sDriverName)
  236.     {
  237.         sDriverName = sDriverFile;
  238.     }
  239.     else if (!sDriverFile)
  240.     {
  241.         sDriverFile = sDriverName;
  242.     }
  243.  
  244.     int rc = 0;
  245.     loadDriver = NewLoadDriver (fVxd);
  246.     if (!loadDriver)
  247.     {
  248.         printf ("ERROR. please check the following:\n");
  249.         printf ("  Opreating system: This program can run only on Win95/Win98/WinNT\n");
  250.         printf ("  -vxd option can not be used in WinNT\n");
  251.         rc = 1;
  252.         goto Exit;
  253.     }
  254.     if (loadDriver->HasMsg())
  255.         printf ("%s", loadDriver->GetLastMsg());
  256.     if (loadDriver->HasErr())
  257.     {
  258.         rc = 1;
  259.         goto Exit;
  260.     }
  261.  
  262.     if (!loadDriver->Init(sDriverName, sDriverFile, startup))
  263.     {
  264.         printf ("Init failed\n");
  265.         rc = 1;
  266.         goto Exit;
  267.     }
  268.  
  269.     if (i==argc)
  270.     {
  271.         printf ("nothing to do!\n");
  272.         rc = 0;
  273.         goto Exit;
  274.     }
  275.  
  276.     for (; i<argc; i++)
  277.     {
  278.         if (stricmp(argv[i],"create")==0)
  279.         {
  280.             printf ("Creating driver entry... ");
  281.             if (!loadDriver->Create())
  282.             {
  283.                 printf ("Failed\n");
  284.                 printf ("%s", loadDriver->GetLastMsg());
  285.             }
  286.             else printf ("OK\n");
  287.         }
  288.         else if (stricmp(argv[i],"delete")==0)
  289.         {
  290.             printf ("Deleting driver entry... ");
  291.             if (!loadDriver->Delete())
  292.             {
  293.                 printf ("Failed\n");
  294.                 printf ("%s", loadDriver->GetLastMsg());
  295.             }
  296.             else printf ("OK\n");
  297.         }
  298.         else if (stricmp(argv[i],"start")==0)
  299.         {
  300.             printf ("Starting driver... ");
  301.             if (!loadDriver->Start())
  302.             {
  303.                 printf ("Failed\n");
  304.                 printf ("%s", loadDriver->GetLastMsg());
  305.             }
  306.             else printf ("OK\n");
  307.         }
  308.         else if (stricmp(argv[i],"stop")==0)
  309.         {
  310.             printf ("Stopping driver... ");
  311.             if (!loadDriver->Stop())
  312.             {
  313.                 printf ("Failed\n");
  314.                 printf ("%s", loadDriver->GetLastMsg());
  315.             }
  316.             else printf ("OK\n");
  317.         }
  318.         else if (stricmp(argv[i],"install")==0)
  319.         {
  320.             printf ("Creating driver entry... ");
  321.             if (!loadDriver->Create())
  322.             {
  323.                 printf ("Failed\n");
  324.                 printf ("%s", loadDriver->GetLastMsg());
  325.             }
  326.             else printf ("OK\n");
  327.             printf ("Starting driver entry... ");
  328.             if (!loadDriver->Start())
  329.             {
  330.                 printf ("Failed\n");
  331.                 printf ("%s", loadDriver->GetLastMsg());
  332.             }
  333.             else printf ("OK\n");
  334.         }
  335.         else if (stricmp(argv[i],"remove")==0)
  336.         {
  337.             printf ("Stopping driver entry... ");
  338.             if (!loadDriver->Stop())
  339.             {
  340.                 printf ("Failed\n");
  341.                 printf ("%s", loadDriver->GetLastMsg());
  342.             }
  343.             else printf ("OK\n");
  344.             printf ("Deleting driver entry... ");
  345.             if (!loadDriver->Delete())
  346.             {
  347.                 printf ("Failed\n");
  348.                 printf ("%s", loadDriver->GetLastMsg());
  349.             }
  350.             else printf ("OK\n");
  351.         }
  352.         else
  353.         {
  354.             printf ("unknown option %s\n",argv[i]);
  355.             goto Exit;
  356.         }
  357.     }
  358.  
  359. Exit:
  360.     delete loadDriver;
  361.     if (!fQuiet)
  362.         OutputMessage();
  363.     return rc;
  364. }
  365.  
  366.  
  367.  
  368.